package com.ruoyi.system.task;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author Felix
 */
@AllArgsConstructor
@Component
@Slf4j
public class ReservationOrderPayTimeoutJob {


    private final RedissonClient redissonClient;

    /**
     * 预约订单-支付超时
     * 项目启动后延迟10秒后执行
     */
//    @Scheduled(initialDelay = 10 * 1000, fixedRate = 60 * 1000)
    public void reservationOrderPayTimeoutJob() {
        String lockKey = "order_pay_timeout_job";
        RLock lock = redissonClient.getLock(lockKey);
        try {
            if (lock.tryLock()) {
                log.info("【预约订单-支付超时】【Job】开始");
//                baseReservationService.reservationOrderPayTimeoutJob();
                log.info("【预约订单-支付超时】【Job】结束");
            } else {
                log.info("【预约订单-支付超时】【Job】获取锁失败，结束");
            }
        } finally {
            if (null != lock && lock.isHeldByCurrentThread()) {
                log.info("【预约订单-支付超时】释放锁");
                lock.unlock();
            }
        }
    }
}
